﻿using System.Web.Mvc;
using yiCommerce.Admin;
using yiCommerce.Core.Data;
using yiCommerce.Core.Domain.Customers;
using yiCommerce.Core.Domain.Orders;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.Shops;
using yiCommerce.Service.Customers;

namespace yiCommerce.Plugin.Install.Controllers
{
    public class InstallController: Controller
    {
        private IRepository<CustomerRole> _customerRoleRepository;
        private IRepository<Customer> _customerRepository;
        private IRepository<Shop> _shopRepository;
        private IRepository<ProductAttribute> _productAttributeRepository;
        private IRepository<SpecificationAttribute> _speficationAttributreRepostiory; 
        private IRepository<SpecificationAttributeOption> _speficationAttributreOptionRepostiory;
        private IRepository<Order> _orderRep;

        public InstallController(IRepository<CustomerRole> customerRoleRepository, IRepository<Customer> customerRepository, IRepository<Shop> shopRepository, IRepository<ProductAttribute> productAttributeRepository, IRepository<SpecificationAttribute> speficationAttributreRepostiory, IRepository<SpecificationAttributeOption> speficationAttributreOptionRepostiory)
        {
            _customerRoleRepository = customerRoleRepository;
            _customerRepository = customerRepository;
            _shopRepository = shopRepository;
            _productAttributeRepository = productAttributeRepository;
            _speficationAttributreRepostiory = speficationAttributreRepostiory;
            _speficationAttributreOptionRepostiory = speficationAttributreOptionRepostiory;
        }

        public ActionResult Index()
        {

            DeleteOrders();
            
            //InstallCustomerRole();
            //var customerId = InstallCustomer();
            //InstallSpecificationAttributes();
            //InstallShop(customerId);

            //InstallProductAttribute();

            //return View();
            //return View("PluginSysWebRegForm.Views.RegForm.Index");
            return View("yiCommerce.Plugin.Install.Views.Install.Index");


        }

        private void DeleteOrders()
        {
            var orderList = _orderRep.Table;
            foreach (var order in orderList)
            {
                _orderRep.Delete(order);
            }

        }

        private void InstallCustomerRole()
        {
            var role = new CustomerRole()
                {
                    Active = true,
                    SystemName = "Guests"
                };
            _customerRoleRepository.Insert(role);

            role = new CustomerRole()
            {
                Active = true,
                SystemName = "Registered"
            };
            _customerRoleRepository.Insert(role);
        }

        private void InstallSpecificationAttributeOptions()
        {
            
        }
        private void InstallSpecificationAttributes()
        {

            var p = new SpecificationAttribute()
                {
                    DisplayOrder = 1,
                    Name = "Screen Size"
                };
            _speficationAttributreRepostiory.Insert(p);

            var o = new SpecificationAttributeOption()
                {
                    SpecificationAttributeId = p.Id,
                    Name = "10.0"
                };

            _speficationAttributreOptionRepostiory.Insert(o);

            o = new SpecificationAttributeOption()
            {
                SpecificationAttributeId = p.Id,
                Name = "20.0"
            };

            _speficationAttributreOptionRepostiory.Insert(o);

            p = new SpecificationAttribute()
                {
                    DisplayOrder = 2,
                    Name = "CPU Type"
                };
            _speficationAttributreRepostiory.Insert(p);


            o = new SpecificationAttributeOption()
            {
                SpecificationAttributeId = p.Id,
                Name = "Intel"
            };

            _speficationAttributreOptionRepostiory.Insert(o);
            o = new SpecificationAttributeOption()
            {
                SpecificationAttributeId = p.Id,
                Name = "AMD"
            };

            _speficationAttributreOptionRepostiory.Insert(o);
        }

        private void InstallProductAttribute()
        {
            var pa = new ProductAttribute()
                {
                    Description = "Color",
                    Name = "Color"
                };

            _productAttributeRepository.Insert(pa);
        }

        private int InstallCustomer()
        {
            var customer = new Customer()
                {
                    Username = "cc",
                    Email = "cc@cc.com",
                    Password = "123456",

                };
            _customerRepository.Insert(customer);
            return customer.Id;
        }

        private void InstallShop(int customerId)
        {

            var shop = new Shop()
                {
                    ShopName = "hello world",
                    CustomerId = customerId
                };
            _shopRepository.Insert(shop);

        }

        private void InstallShippingAddress()
        {
            
        }




      
    }
}
